$(function () {
    window.isMobile = !!("ontouchstart" in window);

    //new LazyLoad({elements_selector: ".lazy"});

    if (!isMobile) {
        // 搜索框
        $("input[name='search']").on("focus", function () {
            $(this).closest(".form-search").addClass("focused");
        }).on("blur", function (e) {
            var that = this;
            setTimeout(function () {
                $(that).closest(".form-search").removeClass("focused");
            }, 500);
        });
    }

    // 点击收藏
    $(".addbookbark").attr("rel", "sidebar").click(function () {
        var url = location.href;
        var title = $(this).attr("title") || document.title;
        if (/firefox/i.test(navigator.userAgent)) {
        } else if (window.external && window.external.addFavorite) {
            window.external.addFavorite(url, title);
        } else if (window.sidebar && window.sidebar.addPanel) {
            window.sidebar.addPanel(title, url, "");
        } else {
            var touch = (navigator.userAgent.toLowerCase().indexOf('mac') != -1 ? 'Command' : 'CTRL');
            layer.msg('请使用 ' + touch + ' + D 添加到收藏夹.');
        }
        return false;
    });

    // 点击收藏
    $(document).on('click', '.btn-collect', function (e) {
        var that = this;
        SHOP.api.ajax({
            url: "/addons/shop/ajax/collect",
            data: {goods_id: $(this).data("id")}
        }, function (data, ret) {
            $("span", that).text("已收藏");
            layer.msg(ret.msg);
            return false;
        });
    });

    if (typeof wx !== 'undefined') {

        //分享参数配置
        var shareConfig = {
            title: $("meta[property='og:title']").attr("content") || document.title,
            description: $("meta[property='og:description']").attr("content") || "",
            url: $("meta[property='og:url']").attr("content") || location.href,
            image: $("meta[property='og:image']").attr("content") || ""
        };

        //微信公众号内分享
        shareConfig.url = location.href;
        SHOP.api.ajax({
                url: "/addons/shop/ajax/share",
                data: {url: shareConfig.url},
                loading: false
            }, function (data, ret) {
                try {
                    wx.config({
                        appId: data.appId,
                        timestamp: data.timestamp,
                        nonceStr: data.nonceStr,
                        signature: data.signature,
                        jsApiList: [
                            "onMenuShareTimeline", //分享给好友
                            "onMenuShareAppMessage", //分享到朋友圈
                            "onMenuShareQQ", //分享到QQ
                            "onMenuShareWeibo" //分享到微博
                        ]
                    });
                    var shareData = {
                        title: shareConfig.title,
                        desc: shareConfig.description,
                        link: shareConfig.url,
                        imgUrl: shareConfig.image,
                        success: function () {
                            layer.closeAll();
                        },
                        cancel: function () {
                            layer.closeAll();
                        }
                    };
                    wx.ready(function () {
                        wx.onMenuShareTimeline(shareData);
                        wx.onMenuShareAppMessage(shareData);
                        wx.onMenuShareQQ(shareData);
                        wx.onMenuShareWeibo(shareData);
                    });

                } catch (e) {

                }
                return false;
            }, function () {
                return false;
            }
        );
    }

    // 点击分享
    $(document).on('click', '.btn-share', function (e) {
        var that = this;
        var data = $(that).data();
        if (typeof wx != 'undefined') {
            layer.msg("请点击右上角的●●●进行分享");
        } else {
            layer.open({
                title: '分享',
                content: '<div class="social-share text-center mt-2 mb-1"><div class="share-qrcode"></div><p class="small">请使用微信扫一扫进行分享</p></div>',
                btn: false,
                success: function (layero) {
                    $(".share-qrcode").qrcode({text: $(that).data("url") || location.href});
                    $('.social-share', layero).data(data).share({sites: 'qzone,qq,weibo,douban'});
                }
            });
        }
        return false;
    });

    //领取优惠券
    $(document).on('click', '.btn-coupon', function () {
        let id = $(this).data('name');
        SHOP.api.ajax({
            url: "/addons/shop/coupon/drawCoupon",
            data: {id: id}
        }, function (data, ret) {
            setTimeout(function () {
                window.location.reload();
            }, 1500);
        });
    });
// 倒计时
    $('[data-countdown]').each(function () {
        var that = this;
        var $this = $(this), finalDate = parseInt($(this).data('countdown'));
        if (finalDate > 0) {
            finalDate = isNaN(finalDate) ? finalDate : new Date().getTime() + finalDate * 1000;
            var format = $(that).data("format");
            $this.on('finish.countdown', function () {
                if (typeof $this.data("finish") == 'function') {
                    $this.data("finish").call($this);
                } else {
                    location.reload();
                }
            }).countdown(finalDate, function (event) {
                $this.html(event.strftime(format) || event.strftime('%D天%H时%M分%S秒'));
            });
        }
    });
});
